﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
using Host.DataContext;
using log4net;
using log4net.Config;

namespace Host
{
    public class QuizService : IQuizService
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(QuizService));

        public QuizService()
        {
            XmlConfigurator.Configure();
            log.Info("Start");
        }

        public List<BLL.Quiz> GetQuizesByDifficulty(int difficulty)
        {
            try
            {
                using (var db = new QuizDB())
                {
                    if (db.Quizes.Any(q => q.Difficulty == difficulty))
                    {
                        log.Debug("GetQuizesByDifficulty:" + difficulty);
                        return db.Quizes.Where(q => q.Difficulty == difficulty)
                            .Select(q => new BLL.Quiz
                                             {
                                                 ID = q.ID,
                                                 Name = q.Name,
                                                 Difficulty = q.Difficulty
                                             }).ToList();
                    }
                }
            }
            catch (Exception e)
            {
                log.Error(e.Message, e);
            }
            return null;
        }
        
        public BLL.Quiz GetQuiz(int id)
        {
            try
            {
                using (var db = new QuizDB())
                {
                    var quiz = db.Quizes.FirstOrDefault(q => q.ID == id);
                    if (quiz != null)
                    {
                        log.Debug("Get Quiz Question:" + id);
                        var questions = db.Questions.Where(q => q.QuizID == id);
                        return new BLL.Quiz
                                   {
                                       Name = quiz.Name,
                                       Difficulty = quiz.Difficulty,
                                       ID = quiz.ID,
                                       Description = quiz.Description,
                                       Questions = questions.Select(q => new BLL.Question
                                                                                  {
                                                                                      ID = q.ID,
                                                                                      QuizID = q.QuizID,
                                                                                      Point = q.Point,
                                                                                      AnswerA = q.AnswerA,
                                                                                      AnswerB = q.AnswerB,
                                                                                      AnswerC = q.AnswerC,
                                                                                      AnswerD = q.AnswerD,
                                                                                      RightAnswer = q.RightAnswer,
                                                                                      Text = q.Text
                                                                                  }).ToList()
                                   };
                    }
                }
            }
            catch(Exception e)
            {
                log.Error(e.Message,e);
            }
            return null;
        }

        public void SaveScore(string name, int score, int quizId)
        {
            try
            {
                using (var db = new QuizDB())
                {
                    var newScore = new Highscore
                                       {
                                           QuizID = quizId,
                                           Points = score,
                                           Name = name
                                       };

                    db.Highscores.Add(newScore);
                    db.SaveChanges();
                }
            }
            catch (Exception e)
            {
                log.Error(e.Message, e);
            }
        }

        public List<BLL.Highscore> GetHighscores()
        {
            try
            {
                using (var db = new QuizDB())
                {
                    return db.Highscores.Select(hs => new BLL.Highscore
                                                          {
                                                              Id = hs.ID,
                                                              Name = hs.Name,
                                                              Points = hs.Points,
                                                              QuizId = hs.QuizID,
                                                              QuizName = hs.Quiz.Name
                                                          }).ToList();
                }
            }
            catch (Exception e)
            {
                log.Error(e.Message, e);
            }
            return null;
        }
    }
}